import gosu.TextInput;

import org.jruby.*;
import org.jruby.runtime.*;
import org.jruby.runtime.builtin.IRubyObject;
import org.jruby.javasupport.JavaUtil;
import org.jruby.anno.JRubyMethod;
import static org.jruby.RubyNumeric.*;


public class TextInputProxy extends RubyObject {
  private static ObjectAllocator ALLOCATOR = new ObjectAllocator() {
    public IRubyObject allocate(Ruby runtime, RubyClass klass) {
      return new TextInputProxy(runtime, klass);
    }
  };

  private TextInput _input;

  private final Ruby _runtime;

  public static void createTextInputClass(Ruby runtime) {
    RubyModule mGosu = runtime.getModule("Gosu");
    RubyClass cTextInput = mGosu.defineClassUnder("TextInput", runtime.getObject(), ALLOCATOR);

    cTextInput.defineAnnotatedMethods(TextInputProxy.class);
  }


  public TextInputProxy(Ruby runtime, RubyClass type) {
    super(runtime, type);
    _runtime = runtime;
  }


  public TextInput getTextInput() { return _input; }
  public void setTextInput(TextInput input) { _input = input; }


  @JRubyMethod(name = "initialize",
               visibility = Visibility.PRIVATE)
  public IRubyObject initialize() {
    _input = new TextInput();
    return this;
  }


  @JRubyMethod(name = "text")
  public IRubyObject getText() {
    return _runtime.newString(_input.getText());
  }


  @JRubyMethod(name = "text=",
               required = 1)
  public IRubyObject setText(IRubyObject arg) {
    _input.setText(arg.toString());
    return arg;
  }


  @JRubyMethod(name = "caret_pos")
  public IRubyObject getCaretPos() {
    return _runtime.newFixnum(_input.getCaretPos());
  }


  @JRubyMethod(name = "selection_start")
  public IRubyObject getSelectionStart() {
    return _runtime.newFixnum(_input.getSelectionStart());
  }

}
